/**
 * Created by rockyren on 14-8-27.
 */
/**
 * @class 像素管理类
 * 注意每四个下标表示一个颜色
 * 获取的下标参数采用一般下标
 */

define(function(require,exports,module){
  var ColorHandle = require('./ColorHandle');
  var cfg = require('../config');

  function imgDataManagement(imgData,canvasX,canvasY) {
    this.imgData = imgData;
    //记录画布的左上角,以便复制到画布
    this.canvasX = canvasX;
    this.canvasY = canvasY;
  }

  module.exports = imgDataManagement;

  /**
   * @method 根据下标获取某像素的颜色
   * @param x
   * @param y
   * @returns {ColorHandle} 返回颜色类
   */
  imgDataManagement.prototype.getImageColor = function(x,y) {
    var imgIndex = (x + y * cfg.AREA_WIDTH)*4;
    var red = this.imgData.data[imgIndex];
    var green = this.imgData.data[imgIndex + 1];
    var blue = this.imgData.data[imgIndex + 2];
    var alpha = this.imgData.data[imgIndex + 3];
    //return {r:red,g:green,b:blue,alp:alpha};
    return new ColorHandle(red,green,blue,alpha);
  };

  /**
   * @method 根据下标和新颜色对象设置像素颜色
   * @param x
   * @param y
   * @param newColor 需要设置的颜色
   */
  imgDataManagement.prototype.setImageColor = function(x,y,newColor) {
    var imgIndex = (x + y * cfg.AREA_WIDTH) * 4;
    this.imgData.data[imgIndex] = newColor.red;
    this.imgData.data[imgIndex + 1] = newColor.green;
    this.imgData.data[imgIndex + 2] = newColor.blue;
    this.imgData.data[imgIndex + 3] = newColor.alpha;
  };



  /**
   * @method 复制像素到画布上,复制像素到画布的职责交给填充类
   */
  imgDataManagement.prototype.putImageToCanvas = function() {
    cfg.CTX.putImageData(this.imgData,this.canvasX,this.canvasY);
  }

});